  package com.yssoft.controller.supervise;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.yssoft.entity.production.TWorksheet;
import com.yssoft.service.core.AreaService;
import com.yssoft.service.core.DdDetailService;
import com.yssoft.service.core.EnterpriseService;
import com.yssoft.service.core.ProductClassifyService;
import com.yssoft.service.production.ProductionProcessService;
import com.yssoft.service.production.WorkSheetService;
import com.yssoft.service.supervise.ProductionDetectionService;
import com.yssoft.utils.AppUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.jws.WebParam;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @创建人: 陈翼东
 * @时间: 2018/6/11 0011 上午 10:26
 * @说明: 生产检测
 */
@RequestMapping("productionDetection")
@Controller
public class ProductionDetectionController {
    
    
    @Resource
    private ProductionDetectionService productionDetectionService;
    
    @Resource
    private EnterpriseService enterpriseService;
    
    @Resource
    private ProductionProcessService productionProcessService;
    
    @Autowired
    private ProductClassifyService productClassifyService;
    
    @Resource
    private AreaService areaService;
    
    /**
     *
     * @创建人: 陈翼东
     * @时间: 2018/6/11 0011 上午 10:26
     * @说明: 跳转到生产检测列表页面
     * @return
     */
    @RequestMapping("detectionList")
    public String detectionList(Model model){
        List<Map<String, Object>> jsonArray = productClassifyService.queryProClaTree();
        String areaTree = areaService.queryAreaTree();
        model.addAttribute("mJson", JSONArray.toJSON(jsonArray));
        model.addAttribute("areaTree", areaTree);
        return "/supervise/productionDetection/detectionList";
    }
    /**
     * 
        * @Title: detail
        * @Description: 查看详情
        * @param @param model
        * @param @return    参数
        * @return String    返回类型
        * @throws
     */
    @RequestMapping("detectionDetail")
    public String detail(Model model,Integer worksheetId) {
    	 Map<String, Object> map=productionDetectionService.queryDetectionDetail(worksheetId);
         model.addAttribute("map", map);
    	return "/supervise/productionDetection/detectionDetail";
    }
    /**
     * @创建人: 陈翼东
     * @时间: 2018/6/11 0011 上午 10:26
     * @说明: 查询加工单信息
     * @param page
     * @param limit
     * @param worksheetTime 起止时间
     * @param area 地区
     * @param mType 商品类型
     * @param productionCompanyName 生产企业名称
     * @return
     */
    @RequestMapping("detectionListPage")
    @ResponseBody
    public JSONObject detectionListPage(@RequestParam(name = "page", defaultValue = "0") int page, @RequestParam(name = "limit", defaultValue = "10") int limit,
                                    String worksheetTime, String area,String mType,String productionCompanyName){
        Map<String,Object> params=new HashMap<>();
       
        if(worksheetTime!=null && worksheetTime.indexOf(" - ")>0){
            String[] time= worksheetTime.split(" - ");
            params.put("startTime",time[0].trim()); //起止时间
            params.put("endTime",time[1].trim()); //起止时间
        }else{
            params.put("startTime",null); //起止时间
            params.put("endTime",null); //起止时间
        }
        params.put("area",area); //地区
        params.put("mType",mType); //商品类型
        if (productionCompanyName!=null && productionCompanyName.length()>0) {
            String[] productionCompanyIdsArr = productionCompanyName.split(",");
            params.put("productionCompanyIdsArr",productionCompanyIdsArr); //生产企业名称
        }else{
            params.put("productionCompanyIdsArr",null); //生产企业名称
        }
        JSONObject jsonObject = productionDetectionService.queryWorksheetPage(page, limit,params);
        return jsonObject;
    }
    
    /**
     * @创建人: 陈翼东
     * @时间: 2018/6/11 0011 上午 10:26
     * @说明: 查询生产企业dialog
     * @param companyIds
     * @param model
     * @return
     */
    @RequestMapping("queryEnterpriseAll")
    public String queryEnterpriseAll(String companyIds,Model model){
        model.addAttribute("companyIds",companyIds);
        return "supervise/productionDetection/enterpriseListDialog";
    }
    
    /**
     * @创建人: 陈翼东
     * @时间: 2018/6/11 0011 上午 10:26
     * @说明: 查询所有生产企业
     * @return
     */
    @RequestMapping("queryEnterpriseAllPage")
    @ResponseBody
    public JSONObject queryEnterpriseAllPage(int page ,int limit,String productionName ,String companyIds){
        return enterpriseService.queryEnterpriseAllPage(page,limit,productionName,companyIds);
    }
    
    /**
     * @创建人: 陈翼东
     * @时间: 2018/6/11 0011 上午 10:26
     * @说明: 查询加工单工艺流程
     * @param workId
     * @param model
     * @return
     */
    @RequestMapping("technological")
    public String technological(String workId,Model model){
        List<Map<String,Object>>  productionProcessList =productionDetectionService.selectProductionProcessList(workId);
        for (Map<String, Object> stringObjectMap : productionProcessList) {
        	if("".equals(stringObjectMap.get("id")) || stringObjectMap.get("id") == null){
        		continue;
        	}
            List<Map<String, Object>> index = productionProcessService.findIndexAndValueByProcessingTechnic(stringObjectMap.get("id") + "",Integer.parseInt(workId));
            stringObjectMap.put("technicIndexList", index);
        }
        model.addAttribute("productionProcessList",productionProcessList);
        return "supervise/productionDetection/technologicalProcess";
    }
    
    /**
     * @创建人: 陈翼东
     * @时间: 2018/6/12 0011 上午 10:26
     * @说明: 查询加工单领料记录
     * @param workId
     * @param model
     * @return
     */
    @RequestMapping("pickingRecord")
    public String pickingRecord(String workId,Model model){
        List<Map<String, Object>> collarMaterialList = productionDetectionService.findCollarMaterial(workId);
        model.addAttribute("collarMaterialList",collarMaterialList);
        return "/supervise/productionDetection/pickingRecord";
    }
    
    
    
    /**
     * @创建人: 陈翼东
     * @时间: 2018/6/12 0011 上午 10:26
     * @说明: 查询加工单检验报告
     * @param workId
     * @param model
     * @return
     */
    @RequestMapping("inspectionReport")
    public String inspectionReport(String workId,Model model){
        List<Map<String, Object>> inspectionReport = productionDetectionService.selectInspectionReport(workId);
        System.out.println(inspectionReport);
        model.addAttribute("inspectionReportList",inspectionReport);
        return "supervise/productionDetection/inspectionReport";
    }
}
